﻿public class Solution {
    public int MinDistance(string word1, string word2) 
    {
        // 最长公共子序列问题
        int[,] dp = new int[word1.Length + 1, word2.Length + 1];
        
        // 递推公式
        for (int i = 1; i <= word1.Length; i++)
        {
            for (int j = 1; j <= word2.Length; j++)
            {
                if (word1[i - 1] == word2[j - 1]) dp[i, j] = dp[i - 1, j - 1] + 1;
                else dp[i, j] = Math.Max(dp[i - 1, j], dp[i, j - 1]);
            }
        }
        
        return word1.Length + word2.Length - 2 * dp[word1.Length, word2.Length];
    }

    public static void Main(string[] args)
    {
        string word1 = "leetcode", word2 = "etco";
        Solution solution = new Solution();
        int res = solution.MinDistance(word1, word2);
        Console.WriteLine(res);
    }
}